package com.ruoyi.test_emp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.test_emp.domain.TestDept;
import com.ruoyi.test_emp.domain.TestEmp;
import com.ruoyi.test_emp.mapper.TestDeptMapper;
import com.ruoyi.test_emp.service.ITestDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门查询Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-03
 */
@Service
public class TestDeptServiceImpl extends ServiceImpl<TestDeptMapper,TestDept> implements ITestDeptService
{
    @Autowired
    private TestDeptMapper testDeptMapper;

    /**
     * 查询部门查询
     * 
     * @param id 部门查询ID
     * @return 部门查询
     */
    @Override
    public TestDept selectTestDeptById(Integer id)
    {
        return testDeptMapper.selectTestDeptById(id);
    }

    /**
     * 查询部门查询列表
     * 
     * @param testDept 部门查询
     * @return 部门查询
     */
    @Override
    public List<TestDept> selectTestDeptList(TestDept testDept)
    {
        return testDeptMapper.selectTestDeptList(testDept);
    }

    /**
     * 新增部门查询
     * 
     * @param testDept 部门查询
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTestDept(TestDept testDept)
    {
        int rows = testDeptMapper.insertTestDept(testDept);
        insertTestEmp(testDept);
        return rows;
    }

    /**
     * 修改部门查询
     * 
     * @param testDept 部门查询
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTestDept(TestDept testDept)
    {
        testDeptMapper.deleteTestEmpByDeptno(testDept.getId());
        insertTestEmp(testDept);
        return testDeptMapper.updateTestDept(testDept);
    }

    /**
     * 批量删除部门查询
     * 
     * @param ids 需要删除的部门查询ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTestDeptByIds(Integer[] ids)
    {
        testDeptMapper.deleteTestEmpByDeptnos(ids);
        return testDeptMapper.deleteTestDeptByIds(ids);
    }

    /**
     * 删除部门查询信息
     * 
     * @param id 部门查询ID
     * @return 结果
     */
    @Override
    public int deleteTestDeptById(Integer id)
    {
        testDeptMapper.deleteTestEmpByDeptno(id);
        return testDeptMapper.deleteTestDeptById(id);
    }

    /**
     * 新增员工测试信息
     * 
     * @param testDept 部门查询对象
     */
    public void insertTestEmp(TestDept testDept)
    {
        List<TestEmp> testEmpList = testDept.getTestEmpList();
        Integer id = testDept.getId();
        if (StringUtils.isNotNull(testEmpList))
        {
            List<TestEmp> list = new ArrayList<TestEmp>();
            for (TestEmp testEmp : testEmpList)
            {
                testEmp.setDeptno(id);
                list.add(testEmp);
            }
            if (list.size() > 0)
            {
                testDeptMapper.batchTestEmp(list);
            }
        }
    }
}
